﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TotalCrytography.Data;
using System.IO;
using TotalCrytography.Protector;

namespace TotalCrytography
{
    public partial class MainGUI : Form
    {
        
        
        static UserDTO currentUser;
        static string currentPass;
        public static bool isLogin;
        private ChildWindow m_selectedChild;

        public static void SetCurrentUser(UserDTO user)
        {
            currentUser = user;  
        }

        public static UserDTO GetCurrentUser()
        {
            return currentUser;
            
        }

        public static void SetCurrentPass(string pass)
        {
            currentPass = pass;
        }

        public static string GetCurrentPass()
        {
            return currentPass;
        }

        public MainGUI()
        {
            InitializeComponent();
            currentPass = "";
            currentUser = null;
            SetConfig();
        }


        private void SetConfig()
        {
            if (!Directory.Exists(@"C:\Users"))
                Directory.CreateDirectory(@"C:\Users");
            if (!Directory.Exists(@"C:\Users\Public"))
                Directory.CreateDirectory(@"C:\Users\Public");
            FileStream fs = new FileStream(@"C:\Users\Public\database.conf", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            DataProvider.Connect();
            bw.Write(DataProvider.ConnectionString);
            DataProvider.Disconnect();
            bw.Close();
            fs.Close();
        }

        public static string GetConfig()
        {
            FileStream fs = new FileStream(@"C:\Users\Public\database.conf", FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            string strCon = br.ReadString();
            br.Close();
            fs.Close();
            return strCon;
        }

        void SetEnable(bool state)
        {
            toolStripButtonChangeInfo.Enabled = state;
            toolStripButtonExport.Enabled = state;
            toolStripButtonSign.Enabled = state;
            //toolStripButtonCheck.Enabled = state;
            toolStripButtonDecrypt.Enabled = state;
            decryptToolStripMenuItem.Enabled = state;
            signToolStripMenuItem.Enabled = state;
            //checkToolStripMenuItem.Enabled = state;
            changeInforToolStripMenuItem.Enabled = state;
            importToolStripMenuItem1.Enabled = state;
            exportToolStripMenuItem1.Enabled = state;
            childWindowRight.SetEnable(state);
            childWindowLeft.SetEnable(state);
            if (state == false)
            {
                toolStripButtonLogin.Image = Properties.Resources.logout;
                toolStripButtonLogin.Text = "Log in";
            }
            else
            {
                toolStripButtonLogin.Image = Properties.Resources.login;
                toolStripButtonLogin.Text = "Log out";
            }
            isLogin = state;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            childWindowLeft.Init(@"C:\");
            childWindowLeft.ShowListFile();
            childWindowLeft.oppChild = childWindowRight;
            childWindowLeft.Focus();
            m_selectedChild = childWindowLeft;

            childWindowRight.Init(@"C:\");
            childWindowRight.ShowListFile();
            childWindowRight.oppChild = childWindowLeft;

            SetEnable(false);
        }

        private void registerToolStripMenuItemRegister_Click(object sender, EventArgs e)
        {
            RegisterAccountGUI frm = new RegisterAccountGUI();
            frm.Show();
        }

        private void toolStripButtonMove_Click(object sender, EventArgs e)
        {
            m_selectedChild.MoveTo();
        }

        private void toolStripButtonEncrypt_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            EncryptGUI encFrm = new EncryptGUI(m_selectedChild.CurSelectFile);
            if (encFrm.ShowDialog(this) == DialogResult.OK)
            {
                Refresh();
            }
        }

        private void toolStripButtonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Title = "Import";
            openDlg.Filter = "Export files|*.exp|All files|*.*";
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                string path = openDlg.FileName;
                if (UserBUS.Import(path))
                    MessageBox.Show("Import successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                else
                    MessageBox.Show("User information has existed!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            if (currentUser != null)
            {
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Title = "Export";
                saveDlg.Filter = "Export files|*.exp|All files|*.*";
                if (saveDlg.ShowDialog() == DialogResult.OK)
                {
                    string path = saveDlg.FileName;
                    if (UserBUS.Export(currentUser, path, null))
                        MessageBox.Show("Export successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    else
                        MessageBox.Show("Unknown error occurs!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void toolStripButtonRegister_Click(object sender, EventArgs e)
        {
            RegisterAccountGUI registerFrm = new RegisterAccountGUI();
            if (registerFrm.ShowDialog() == DialogResult.OK)
                SetEnable(true); 
        }

        private void toolStripButtonDecrypt_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            DecryptGUI decFrm = new DecryptGUI(m_selectedChild.CurSelectFile,currentPass,currentUser.PrivateKey);
            if (decFrm.ShowDialog(this) == DialogResult.OK)
            {
                Refresh();
            }

        }

        private void toolStripButtonChangeInfo_Click(object sender, EventArgs e)
        {
            ChangeInfoGUI changeInfoFrm = new ChangeInfoGUI();
            changeInfoFrm.Show();
        }

        private void toolStripButtonLogin_Click(object sender, EventArgs e)
        {
            if (isLogin)
                Logout();
            else
                Login() ;
        }
        public void Logout()
        {
            if (MessageBox.Show("Do you really want to log out ?", "Log out", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                SetEnable(false);
            }
        }

        public void Login()
        {
            LoginGUI loginFrm = new LoginGUI();
            if (loginFrm.ShowDialog(this) == DialogResult.OK)
            {
                if (isLogin == true)
                {
                    toolStripButtonLogin.Image = Properties.Resources.login;
                    SetEnable(true);
                }
            }
        }

        private void toolStripButtonSign_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            Sign();
        }

        public void Sign()
        {
            SignGUI signFrm = new SignGUI(m_selectedChild.CurSelectFile);
            if (signFrm.ShowDialog(this) == DialogResult.OK)
                Refresh();
        }

        private void toolStripButtonCheck_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            CheckGUI checkFrm = new CheckGUI(m_selectedChild.CurSelectFile);
            checkFrm.Show();
        }


        private void changeInforToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonChangeInfo_Click(sender, e);
        }

        private void importToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            toolStripButtonImport_Click(sender, e);
        }

        private void exportToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            toolStripButtonExport_Click(sender, e);
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            childWindowLeft.ShowListFile();
            childWindowRight.ShowListFile();
        }

        private void childWindowLeft_Enter(object sender, EventArgs e)
        {
            m_selectedChild = childWindowLeft;
        }

        private void childWindowRight_Enter(object sender, EventArgs e)
        {
            m_selectedChild = childWindowRight;
        }


        private void encryptToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            EncryptGUI frm = new EncryptGUI(childWindowLeft.CurSelectFile);
            frm.Show();
            Refresh();
        }

        private void decryptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            DecryptGUI frm = new DecryptGUI(childWindowLeft.CurSelectFile,currentPass,currentUser.PrivateKey);
            frm.Show();
            Refresh();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (childWindowRight.Focused)
                childWindowRight.ShowListFile();
            else if (childWindowLeft.Focused)
                childWindowLeft.ShowListFile();
        }

        private void toolStripButtonMakeDir_Click(object sender, EventArgs e)
        {
            m_selectedChild.NewFolder();
        }

        private void toolStripButtonCopy_Click(object sender, EventArgs e)
        {
            m_selectedChild.CopyTo();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            m_selectedChild.Delete();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            m_selectedChild.NewFolder();
        }

        private void copyToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_selectedChild.CopyTo();
        }

        private void moveToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_selectedChild.MoveTo();
        }

        private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            m_selectedChild.Delete();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void zipToolStripMenuZip_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            ZipGUI frm = new ZipGUI(m_selectedChild.CurSelectFile);
            if (frm.ShowDialog() == DialogResult.OK)
                Refresh();
        }

        private void unzipToolStripMenuUnzip_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            UnzipGUI frm = new UnzipGUI(m_selectedChild.CurSelectFile[0]);
            if (frm.ShowDialog() == DialogResult.OK)
                Refresh();
        }

        private void childWindowLeft_Click(object sender, EventArgs e)
        {
            m_selectedChild = childWindowLeft;
        }

        private void childWindowRight_Click(object sender, EventArgs e)
        {
            m_selectedChild = childWindowRight;
        }

        private void signToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Sign();
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            m_selectedChild.CopyTo();
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            m_selectedChild.MoveTo();
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            m_selectedChild.NewFolder();
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            m_selectedChild.Delete();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MainGUI_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < m_selectedChild.CurSelectFile.Count; i++)
            {
                if (File.Exists(m_selectedChild.CurSelectFile[i]))
                    System.Diagnostics.Process.Start(@"Notepad2\Notepad2.exe", m_selectedChild.CurSelectFile[i]);
            }
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < m_selectedChild.CurSelectFile.Count; i++)
            {
                if (File.Exists(m_selectedChild.CurSelectFile[i]))
                    System.Diagnostics.Process.Start(@"Notepad2\Notepad2.exe", m_selectedChild.CurSelectFile[i]);
            }
        }

        private void toolStripButtonNotepad_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"Notepad2\Notepad2.exe");
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutGUI aboutFrm = new AboutGUI();
            aboutFrm.Show();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (m_selectedChild.CurSelectFile.Count == 0)
                return;
            ZipGUI zipFrm = new ZipGUI(m_selectedChild.CurSelectFile);
            if (zipFrm.ShowDialog() == DialogResult.OK)
                m_selectedChild.ShowListFile();
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < m_selectedChild.CurSelectFile.Count; i++)
            {
                UnzipGUI unzipFrm = new UnzipGUI(m_selectedChild.CurSelectFile[i]);
                unzipFrm.ShowDialog();
            }
             m_selectedChild.ShowListFile();
        }

        private void addinWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("WordAddInCrytography.vsto");
                MessageBox.Show("Add plug-in successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            catch (System.Exception ex)
            {
            	
            }
        }

    }
}
